package com.example.blog.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.entity.Article;
import com.example.blog.entity.ArticleDTO;
import com.example.blog.entity.Category;
import com.example.blog.mapper.ArticleMapper;
import com.example.blog.service.IArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 刘扬新
 * @since 2023-06-27
 */
@Service
@AllArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {


    @Resource
    private ArticleMapper articleMapper;


    /**
     * 主页文章查询 分页
     * 联表查询出页面各个板块内容
     * * @return
     */
    public List<Map<String, Object>> selectPage( int page,int size) {
        //分页查询
        IPage<ArticleDTO> selectJoinPage = articleMapper.selectJoinPage(
                new Page<ArticleDTO>(page, size),
                ArticleDTO.class,
                //分页表，字段
                new MPJLambdaWrapper<Article>()
                        .selectAll(Article.class)
                        .select(Category::getName)
                        .leftJoin(Category.class, Category::getId, Article::getCategoryId)
                        .eq(Article::getStatus, 1)
                        .orderByDesc(Article::getCreateTime));

        //接收数据
        List<ArticleDTO> pageRecords = selectJoinPage.getRecords();

        /**
         * map
         * article：{}
         * creatTime
         */
        List<Map<String, Object>> list = new ArrayList<>();

        // 时间戳转化  将时间转化之后放到新的map
        for (int i = 0; i < pageRecords.size(); i++) {
            ArticleDTO article = pageRecords.get(i);
            LocalDateTime createTime = article.getCreateTime();
            String time = createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            Map<String, Object> map = new HashMap<>();
            map.put("createTime", time);
            map.put("article", article);
            list.add(map);
        }
        return list;

    }

    /**
     * 分页查询(根据栏目id)
     * 联表查询出页面各个板块内容
     * * @return
     */
    public List<Map<String, Object>> selectPage(int id,int currentPage,int pagesize ) {
        //分页查询
        IPage<ArticleDTO> selectJoinPage = articleMapper.selectJoinPage(
                new Page<ArticleDTO>(currentPage, pagesize),
                ArticleDTO.class,
                //分页表，字段
                new MPJLambdaWrapper<Article>()
                        .selectAll(Article.class)
                        .select(Category::getName)
                        .leftJoin(Category.class, Category::getId, Article::getCategoryId)
                        .eq(Category::getId, id)
                        .eq(Article::getStatus,1)
                        .orderByDesc(Article::getCreateTime));

        //接收数据
        List<ArticleDTO> pageRecords = selectJoinPage.getRecords();
        long total = selectJoinPage.getTotal();

        /**
         * map
         * article：{}
         * creatTime
         */
        List<Map<String, Object>> list = new ArrayList<>();

        // 时间戳转化  将时间转化之后放到新的map
        for (int i = 0; i < pageRecords.size(); i++) {
            ArticleDTO article = pageRecords.get(i);
            LocalDateTime createTime = article.getCreateTime();
            String time = createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            Map<String, Object> map = new HashMap<>();
            map.put("createTime", time);
            map.put("article", article);
            map.put("total",total);
            list.add(map);
        }
        return list;
    }

    /**
     * 根据id查询详细
     * * @return
     */
    public List<Map<String, Object>> selectById(int id) {
        ArticleDTO articleDTO = articleMapper.selectJoinOne(ArticleDTO.class,
                new MPJLambdaWrapper<Article>()
                        .selectAll(Article.class)
                        .select(Category::getName)
                        .leftJoin(Category.class, Category::getId, Article::getCategoryId)
                        .eq(Article::getId, id)
                        .eq(Article::getStatus, 1));

        /**
         * map
         * article：{}
         * creatTime
         */
        List<Map<String, Object>> list = new ArrayList<>();

        // 时间戳转化  将时间转化之后放到新的map
            LocalDateTime createTime = articleDTO.getCreateTime();
            String time = createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            Map<String, Object> map = new HashMap<>();
            map.put("createTime", time);
            map.put("article", articleDTO);
            list.add(map);
        return list;
    }

}
